From 82907dd0b551efd24e2f0f3d9bede93152a7c7b5 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Sat, 16 Mar 2024 16:17:04 -0500 Subject: [PATCH] Change name: instance_method_mock_fixture() -> late_instance_mock_fixture() --- src/pgwui_develop/testing.py | 10 ++++++---- tests/test_testing.py | 22 +++++++++++++--------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/pgwui_develop/testing.py b/src/pgwui_develop/testing.py index a704536..8c81abe 100644 --- a/src/pgwui_develop/testing.py +++ b/src/pgwui_develop/testing.py @@ -1,4 +1,4 @@ -# Copyright (C) 2015, 2018, 2020, 2021 The Meme Factory, Inc. +# Copyright (C) 2015, 2018, 2020, 2021, 2024 The Meme Factory, Inc. # http://www.karlpinc.com/ # This file is part of PGWUI_Develop. @@ -57,9 +57,11 @@ def make_magicmock_fixture(module, method, autouse=False, autospec=False): return fix -def instance_method_mock_fixture(method): - '''Returns a pytest fixture that mocks an instance method of a class. - "method" is a string. +def late_instance_mock_fixture(method, ignore_deprecation=True): + '''Returns a pytest fixture that mocks a instance method. + "method" is the name of the instance or class method. + The function returned by the fixture takes the class instance to + be monkeypatched. The fixture is called by the test function with the class instance that's to be monkeypatched and the mock is returned for the diff --git a/tests/test_testing.py b/tests/test_testing.py index 8e3f7df..a724583 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -1,4 +1,4 @@ -# Copyright (C) 2018, 2019, 2020, 2021 The Meme Factory, Inc. +# Copyright (C) 2018, 2019, 2020, 2021, 2024 The Meme Factory, Inc. # http://www.karlpinc.com/ # This file is part of PGWUI_Develop. @@ -110,26 +110,30 @@ class TestClass(): mocked_method = testing.instance_method_mock_fixture('method_to_mock') +# +# late_instance_mock_fixture() +# +li_mocked_method = testing.late_instance_mock_fixture('method_to_mock') + + @pytest.mark.unittest @pytest.mark.integrationtest -def test_instance_method_mock_fixture(mocked_method): +def test_late_instance_mock_fixture(li_mocked_method): # The mock of the instance method works test_value = 'mocked value' - cls = TestClass() - mocked_method(cls).return_value = test_value + li_mocked_method(test_instance).return_value = test_value - result = cls.method_to_mock() + result = test_instance.method_to_mock() assert result == test_value @pytest.mark.unittest @pytest.mark.integrationtest -def test_instance_method_mock_fixture_unmocked(): - # The test class works after the mocking +def test_late_instance_mock_fixture_unmocked(): + # The test function works after the mocking - cls = TestClass() - result = cls.method_to_mock() + result = test_instance.method_to_mock() assert result == normal_return_value -- 2.34.1